package com.hs.srm.admin.modules.requisition.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.requisition.dao.NproPurchRequestHeaderDao;
import com.hs.srm.admin.modules.requisition.entity.NproPurchRequestHeaderEntity;
import com.hs.srm.admin.modules.requisition.service.NproPurchRequestHeaderService;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("nproPurchRequestHeaderService")
public class NproPurchRequestHeaderServiceImpl extends ServiceImpl<NproPurchRequestHeaderDao, NproPurchRequestHeaderEntity> implements NproPurchRequestHeaderService {

    //注入DAO
    @Autowired
    private NproPurchRequestHeaderDao nproPurcnhRequestHeaderDao;

    @Override
    @DataFilter
    public PageUtils queryPage(Map<String, Object> params) {
        Page<NproPurchRequestHeaderEntity> page = this.selectHeadPage(
                new QueryParam<NproPurchRequestHeaderEntity>(params).getPage(), new EntityWrapper<NproPurchRequestHeaderEntity>(),
                params);
        return new PageUtils(page);
    }

    private Page<NproPurchRequestHeaderEntity> selectHeadPage(Page<NproPurchRequestHeaderEntity> page,
                                                              Wrapper<NproPurchRequestHeaderEntity> wrapper, Map<String, Object> params) {
        page.setRecords(this.nproPurcnhRequestHeaderDao.selectHeadPage(page, wrapper, params));
        return page;
    }

    @Override
    public NproPurchRequestHeaderEntity queryNproPurchRequestHeader() {

        return this.baseMapper.queryNproPurchRequestHeader();
    }

    @Override
    public NproPurchRequestHeaderEntity selectById(String pkId) {

        return this.baseMapper.selectById(pkId);
    }

    @Override
    public NproPurchRequestHeaderEntity findByNo(String purchRequestCode) {
        return this.baseMapper.findByNo(purchRequestCode);
    }

    @Override
    public void updateName(NproPurchRequestHeaderEntity nproPurchRequestHeaderEntity) {

        this.baseMapper.updateName(nproPurchRequestHeaderEntity);
    }

    @Override
    public List<NproPurchRequestHeaderEntity> selectBatchIds(List<String> ids) {

        return this.baseMapper.selectBatchIds(ids);
    }

    //改变货源状态
    @Override
    public boolean changeGoodsStatus(int goodsStatus, String requestCode) {
        NproPurchRequestHeaderEntity headerEntity = new NproPurchRequestHeaderEntity();
        headerEntity.setGoodsStatus(goodsStatus);
        int rows = this.baseMapper.update(headerEntity, new EntityWrapper<NproPurchRequestHeaderEntity>()
                .eq("purch_request_code", requestCode));
        return rows > 0 ? true : false;
    }

    /**
     * 退单后逻辑删除请购单头信息
     *
     * @param requestCode 请购编码
     * @return
     */
    @Override
    public boolean updateByRequestCode(String requestCode) {
        return nproPurcnhRequestHeaderDao.updateByRequestCode(requestCode);
    }

    /**
     * 根据请购编码查询非生产性申请单
     *
     * @param purchRequestCode
     * @return
     */
    @Override
    public NproPurchRequestHeaderEntity selectNproHeader(String purchRequestCode) {
        EntityWrapper<NproPurchRequestHeaderEntity> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("purch_request_code", purchRequestCode);
        NproPurchRequestHeaderEntity one = this.selectOne(queryWrapper);
        if (one != null) {
            return one;
        }
        return null;
    }


}
